package hottargui.domain.delta;

import java.util.*;
import hottargui.domain.*;
import hottargui.domain.standard.StandardTile;

public class DeltaBoardLayoutStrategyImpl implements BoardLayoutStrategy {

	private Vector<ModifiableTile> myBoard;

	private List<Position> positions;
	private List<QuantityOfTileType> qtyOfTileTypes;
	private Position cp;
	private QuantityOfTileType qty;
	private ListIterator<QuantityOfTileType> qtyIterator;

	class QuantityOfTileType {
		private TileType _tt;
		private int _qty;
		public QuantityOfTileType(TileType tt,int qty){
			_tt = tt;
			_qty = qty;
		}

		public boolean equals(Object o){
			if (o.getClass() != QuantityOfTileType.class) { return false; }
			QuantityOfTileType other = (QuantityOfTileType) o;
			return _tt==other._tt;
		}

		public int getQuantity(){
			return _qty;
		}

		public TileType getTileType(){
			return _tt;
		}
	}

	public DeltaBoardLayoutStrategyImpl() {
		myBoard = new Vector<ModifiableTile>();

		initBoardSize();

		createPositions();

		setFixedTiles();

		createTilesToDeploy();

		qtyIterator = qtyOfTileTypes.listIterator();

		while (qtyIterator.hasNext()) {
			qty = qtyIterator.next();
			for (int i = 0;i < qty.getQuantity();i++){
				int index = (int) (positions.size() * Math.random());
				cp = positions.remove(index);
				myBoard.setElementAt(
						new StandardTile(
								qty.getTileType(), 
								PlayerColor.None,
								cp.getColumn(),
								cp.getRow()),
								cp.getColumn()*7+cp.getRow()); 
			}
			qtyIterator.remove();
		}

		replaceWithSettlements();
	}



	public Vector<ModifiableTile> getBoardLayout(){
		return myBoard;	

	}

	private void createPositions(){
		positions = new ArrayList<Position>();

		for (int column = 0;column < 7;column++){
			for (int row = 0;row < 7;row++){
				positions.add(new Position(column,row));
			}
		}

	}

	private void setFixedTiles(){
		myBoard.setElementAt(new StandardTile(TileType.Saltmine, PlayerColor.None, 3,3 ),3*7+3);
		positions.remove(new Position(3,3));
	}

	private void createTilesToDeploy(){
		qtyOfTileTypes = new ArrayList<QuantityOfTileType>();
		qtyOfTileTypes.add(new QuantityOfTileType(TileType.Erg,12));
		qtyOfTileTypes.add(new QuantityOfTileType(TileType.Feshfesh,8));
		qtyOfTileTypes.add(new QuantityOfTileType(TileType.Mountain,6));
		qtyOfTileTypes.add(new QuantityOfTileType(TileType.Oasis,6));
		qtyOfTileTypes.add(new QuantityOfTileType(TileType.Reg,12));
		qtyOfTileTypes.add(new QuantityOfTileType(TileType.Saltlake,4));
	}

	public int getQtyOfPositions(){
		return positions.size();
	}

	public int getQtyOfTileTypesToDeploy(){
		return qtyOfTileTypes.size();
	}

	private void replaceWithSettlements(){
		myBoard.setElementAt(new StandardTile(TileType.Settlement, PlayerColor.None, 0,0 ),0*7+0); 
		myBoard.setElementAt(new StandardTile(TileType.Settlement, PlayerColor.None, 0,6 ),0*7+6); 
		myBoard.setElementAt(new StandardTile(TileType.Settlement, PlayerColor.None, 6,0 ),6*7+0); 
		myBoard.setElementAt(new StandardTile(TileType.Settlement, PlayerColor.None, 6,6 ),6*7+6);
	}

	private void initBoardSize(){
		for (int i = 0;i<49;i++){
			myBoard.add(null);
		}
	}

}
